<?php
/**
* @file $Id: MailingLabels.php 539 2007-05-23 00:35:57Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

$max_cols = 3;
$max_rows = 10;
$to_family = _('To the parents of:');

if($_REQUEST['modfunc']=='save')
{
	if(count($_REQUEST['student']))
	{
		$st_list = '\''.implode('\',\'',$_REQUEST['student']).'\'';
		$extra['WHERE'] = " AND s.STUDENT_ID IN ($st_list)";

		$_REQUEST['mailing_labels']='Y';
		Widgets('mailing_labels');
		$extra['group'] = array('ADDRESS_ID');
		$RET = GetStuList($extra);

		if(count($RET))
		{
			$skipRET = array();
			for($i=($_REQUEST['start_row']-1)*$max_cols+$_REQUEST['start_col']; $i>1; $i--)
				$skipRET[-$i] = array(1=>array('MAILING_LABEL'=>' '));

			$handle = PDFstart();

			$cols = 0;
			$rows = 0;
			$RET = $skipRET+$RET;
			foreach($RET as $i=>$addresses)
			{
				if($rows==0 && $cols==0)
				{
					echo '<!-- MEDIA SIZE 8.5x11in -->';
					echo '<!-- MEDIA TOP 0.5in -->';
					echo '<!-- MEDIA BOTTOM 0.1875in -->';
					echo '<!-- MEDIA LEFT 0.1875in -->';
					echo '<!-- MEDIA RIGHT 0.1875in -->';
					echo '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
				}
				if($i>0)
				{
					if($_REQUEST['to_address']=='student')
					{
						foreach($addresses as $key=>$address)
							$addresses[$key]['MAILING_LABEL'] = $address['FIRST_NAME'].' '.$address['LAST_NAME'].'<BR>'.substr($address['MAILING_LABEL'],strpos($address['MAILING_LABEL'],'<!-- ENDPARENTS -->')+19);
					}
					elseif($_REQUEST['to_address']=='family')
					{
						// if grouping by address, replace people list in mailing labels with students list
						$lasts = array();
						foreach($addresses as $address)
							$lasts[$address['LAST_NAME']][] = $address['FIRST_NAME'];
						$students = '';
						foreach($lasts as $last=>$firsts)
						{
							$student = '';
							$previous = '';
							foreach($firsts as $first)
							{
								if($student && $previous)
									$student .= ','.$previous;
								elseif($previous)
									$student = $previous;
								$previous = $first;
							}
							if($student)
								$student .= '&'.$previous.' '.$last;
							else
								$student = $previous.' '.$last;
							$students .= $student.', ';
						}
						$addresses = array(1=>array('MAILING_LABEL'=>'<SMALL>'.$to_family.'<BR></SMALL>'.substr($students,0,-2).'<BR>'.substr($addresses[1]['MAILING_LABEL'],strpos($addresses[1]['MAILING_LABEL'],'<!-- ENDPARENTS -->')+19)));
					}
				}

				foreach($addresses as $address)
				{
					if(!$address['MAILING_LABEL'])
						continue;

					if($cols < 1)
						echo '<tr>';
					echo '<td width="33.3%" height="83" align="center" valign="middle">';
					echo $address['MAILING_LABEL'];
					echo '</td>';

					$cols++;

					if($cols == $max_cols)
					{
						echo '</tr>';
						$rows++;
						$cols=0;
					}

					if($rows == $max_rows)
					{
						echo '</table><!--NEW PAGE -->';
						$rows=0;
					}
				}
			}

			if ($cols == 0 && $rows == 0)
			{}
			else
			{
				while ($cols !=0 && $cols < $max_cols)
				{
					echo '<td width="33.3%" height="83" align="center" valign="middle">&nbsp;</td>';
					$cols++;
				}
				if ($cols == $max_cols)
				{
					echo '</tr>';
					$rows++;
					$cols=0;
				}
				
				echo '</table>';
			}
			PDFstop($handle);
		}
		else
			BackPrompt(_('No Students were found.'));
	}
}

if(!$_REQUEST['modfunc'])
{
	DrawHeader(ProgramTitle());

	if($_REQUEST['search_modfunc']=='list')
	{
		echo "<FORM action=Modules.php?modname=$_REQUEST[modname]&modfunc=save&include_inactive=$_REQUEST[include_inactive]&_search_all_schools=$_REQUEST[_search_all_schools]&_FOCUS_PDF=true method=POST>";
		DrawHeader('',SubmitButton(_('Create Labels for Selected Students')));

		$left = '<TABLE style="display:inline;">';
		$left .= '<TR><TD><INPUT type=radio name=to_address value="" checked>'._('To all Contacts having Custody').'</TD></TR>';
		$left .= '<TR><TD><INPUT type=radio name=to_address value=student>'._('To each Student').'</TD></TR>';
		$left .= '<TR><TD><INPUT type=radio name=to_address value=family>'._('To "The Parents of"').'</TD></TR>';
		$left .= '</TABLE>';

		$right = '<TABLE style="display:inline;">';
		$right .= '<TR><TD align='.ALIGN_RIGHT.'>'._('Starting row').'</TD><TD><SELECT name=start_row>';
		for($row=1; $row<=$max_rows; $row++)
			$right .=  '<OPTION value="'.$row.'">'.$row;
		$right .=  '</SELECT></TD></TR>';
		$right .= '<TR><TD align='.ALIGN_RIGHT.'>'._('Starting column').'</TD><TD><SELECT name=start_col>';
		for($col=1; $col<=$max_cols; $col++)
			$right .=  '<OPTION value="'.$col.'">'.$col;
		$right .= '</SELECT></TD></TR>';
		$right .= '</TABLE>';
		DrawHeader($left,$right);
	}

	Widgets('all');

	$extra['SELECT'] .= ",s.STUDENT_ID AS CHECKBOX";
	$extra['link'] = array('FULL_NAME'=>false);
	$extra['functions'] = array('CHECKBOX'=>'_makeChooseCheckbox');
	$extra['columns_before'] = array('CHECKBOX'=>'</A><INPUT type=checkbox value=Y name=controller checked onclick="checkAll(this.form,this.form.controller.checked,\'student\');"><A>');
	$extra['options']['search'] = false;
	$extra['new'] = true;

	Search('student_id',$extra);
	if($_REQUEST['search_modfunc']=='list')
	{
		echo '<BR><CENTER>'.SubmitButton(_('Create Labels for Selected Students')).'</CENTER>';
		echo "</FORM>";
	}
}

function _makeChooseCheckbox($value,$title)
{
	return '<INPUT type=checkbox name=student[] value='.$value.' checked>';
}
?>